package com.linmu.controller;

import com.linmu.api.CommonResult;
import com.linmu.entity.User;
import com.linmu.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@ResponseBody
@RestController
public class LoginController {
    final UserServiceImpl userService;

    public LoginController(UserServiceImpl userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/admin/login",method = RequestMethod.GET)
    public String login() {
        //todo 登录页面
        return "login";
    }

    @RequestMapping(value = "/admin/login",method = RequestMethod.POST)
    public CommonResult login(@RequestBody User user) {
        String username = user.getUsername();
        String password = user.getPassword();

        log.info("用户名：" + username + " | " + "密码：" + password);

        long lockTime = userService.getUserLoginLockTime(username);
        if(lockTime > 0) {
            return CommonResult.validateFailed( "登录失败，当前账户锁定，请在" + lockTime + "秒后尝试。" );
        }
        //判断用户名或密码为空
        if (username == null || password == null) {
            return CommonResult.validateFailed("用户名或密码为空");
        }
        user = userService.login(username,password);
        if(user != null) {
            return CommonResult.success( user,"登录成功" );
        }
        else {
            return CommonResult.validateFailed( "登录失败" );
        }
    }
}
